/*
❑ TCSANOW: Changes values immediately.
❑ TCSADRAIN: Changes values when current output is complete.
❑ TCSAFLUSH: Changes values when current output is complete, but discards any input
  currently available and not yet returned in a read call.

*/

#include <termios.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
	struct termios	oldsetting, newsetting;
	char			passwd[16];
	char			account[10];

	/**  get current termios status **/
	tcgetattr(fileno(stdin), &oldsetting);
	
	newsetting = oldsetting;
	newsetting.c_lflag &= ~ECHO;

	printf("Enter passwd:\n");			//NOTICE::::::THIS '\N' CAN'T DISCARD!!!!::::::

	/*** modify struct termios to see if it's no-echo  ****/
	if(tcsetattr(fileno(stdin), TCSAFLUSH, &newsetting) != 0)
		fprintf(stderr, "set attr failed\n");

	else
	{
		read(fileno(stdin), passwd, 16);
		*(strchr(passwd, '\n')) = 0;			//because the last character is '\n' now
		
		/*set the termios back to origin status */

		tcsetattr(fileno(stdin), TCSANOW, &oldsetting);
		fprintf(stdout, "get password: %s\n", passwd);
	}

	
	printf("Account:\n");
	read(fileno(stdin), account, 10);
	*(strchr(account, '\n')) = 0;			//because the last character is '\n' now
	fprintf(stdout, "get account: %s\n", account);

	exit(0);


}
